<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // const nums1 = [4, 9, 5],
      //   nums2 = [9, 4, 9, 8, 4]

      function intersection(nums1, nums2) {
        return nums1.reduce(
          (pre, item) =>
            // console.log(pre),
            !pre.includes(item) && nums2.includes(item) ? [...pre, item] : pre,
          []
        )
      }

      // intersection([4, 9, 5], [9, 4, 9, 8, 4])
      console.log(intersection([4, 9, 5], [9, 4, 9, 8, 4]))
      // console.log(intersection([1, 2, 2, 1], [2, 2]))
      // console.log(intersection([], [2, 2]))
    </script>
  </body>
</html>
